from datetime import datetime
from app import db


class UserVideo(db.Model):
    """用户视频表"""
    __tablename__ = 'user_videos'
    
    id = db.Column(db.Integer, primary_key=True, comment='主键ID')
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, comment='用户ID')
    video_file_id = db.Column(db.Integer, db.ForeignKey('file_uploads.id'), nullable=False, comment='视频文件ID')
    video_url = db.Column(db.String(255), nullable=False, comment='视频URL')
    thumbnail_url = db.Column(db.String(255), comment='缩略图URL')
    duration = db.Column(db.Integer, comment='视频时长（秒）')
    sort_order = db.Column(db.Integer, default=0, comment='排序')
    status = db.Column(db.Integer, default=1, comment='状态：1待审核2通过3拒绝')
    audit_reason = db.Column(db.String(200), comment='审核原因')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    
    # 关联关系
    user = db.relationship('User', backref='videos')
    video_file = db.relationship('FileUpload', backref='user_videos')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'video_file_id': self.video_file_id,
            'video_url': self.video_url,
            'thumbnail_url': self.thumbnail_url,
            'duration': self.duration,
            'sort_order': self.sort_order,
            'status': self.status,
            'status_text': self.get_status_text(),
            'audit_reason': self.audit_reason,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None
        }
    
    def get_status_text(self):
        """获取状态文本"""
        status_map = {
            1: '待审核',
            2: '审核通过', 
            3: '审核拒绝'
        }
        return status_map.get(self.status, '未知')
    
    def __repr__(self):
        return f'<UserVideo {self.id}>' 